#include "user.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static User *user_head=0;
static User *user_last=0;
static int user_number=0;
extern Book *book_head;

int register_user(const char *name,const char *username,const char *password)
{
	User *new_user = (User *)malloc(sizeof(User));
	new_user->name = (char *)malloc(sizeof(strlen(name)));
	strcpy(new_user->name,name);
	new_user->username = (char *)malloc(sizeof(strlen(username)));
	strcpy(new_user->username,username);
	new_user->password = (char *)malloc(sizeof(strlen(password)));
	strcpy(new_user->password,password);
	new_user->books=(BookArray*)malloc(sizeof(BookArray));
	new_user->books->array=0;
	new_user->books->length=0;
	if(user_number==0){
		user_head=(User *)malloc(sizeof(User));
		user_last=user_head;
		user_last->next=new_user;
		user_last=new_user;
		user_last->next=NULL;
	}else{
		user_last->next=new_user;
		user_last=new_user;
		user_last->next=NULL;
	}
	user_number++;
	printf("Register successfully!\n");
	return 0;
}

int store_users()
{
	if(user_number != 0){
	FILE *fp;
	fp = fopen("users.txt", "w+");
	if (!fp) {
		fprintf(stderr, "Failed in opening %s\n", "users.txt");
		return -1;
	}
	fprintf(fp, "Number of users:%d\n", user_number);
	User *current;
	Book *temp;
	for(current=user_head->next;current;current=current->next){
		fprintf(fp, "name:%s\n", current->name);
		fprintf(fp, "username:%s\n", current->username);
		fprintf(fp, "password:%s\n", current->password);
		fprintf(fp, "books borrowed:%d\n", current->books->length);
		if(current->books->length != 0){
			for(temp=current->books->array->next;temp;temp=temp->next){
				fprintf(fp, "id:%d\n", temp->id);
				fprintf(fp, "title:%s\n", temp->title);
				fprintf(fp, "authors:%s\n", temp->authors);
				fprintf(fp, "year:%d\n", temp->year);
				fprintf(fp, "copies:%d\n", temp->copies);
				fprintf(fp, "loan:%d\n", temp->loan);
			}
		}
	}
	User *delete;
	current=user_head;
	while(current){
		delete=current;
		current=current->next;
		free(delete);
	}
	fclose(fp);
	}
	return 0;
}

int load_users()
{
	FILE *fp;
	char buf[100];
	int nuser=0;
 	fp = fopen("users.txt", "r");
	if (!fp){
		fprintf(stderr, "Error in opening users.txt\n");
		return -1;
	}
    //Read the first line
    if (fgets(buf, 100, fp) != NULL){
    	sscanf(buf, "Number of users:%d\n", &nuser);
		user_number=nuser;
		//Read in book entries
		char name[100],username[100],password[100];
		int length,id,year,copies,loan;
		char title[100],authors[100];
		int j;
		user_head=(User*)malloc(sizeof(User));
		user_last=user_head;
		for(int i=0;i<nuser;i++){
			fgets(buf, 100, fp);
			sscanf(buf,"name:%s\n",name);
			fgets(buf, 100, fp);
			sscanf(buf,"username:%s\n",username);
			fgets(buf, 100, fp);
			sscanf(buf,"password:%s\n",password);
			fgets(buf, 100, fp);
			sscanf(buf, "books borrowed:%d\n", &length);

			User *node=(User*)malloc(sizeof(User));
			node->name=(char*)malloc(sizeof(strlen(name)));
			strcpy(node->name,name);
			node->username=(char*)malloc(sizeof(strlen(username)));
			strcpy(node->username,username);
			node->password=(char*)malloc(sizeof(strlen(password)));
			strcpy(node->password,password);
			node->books=(BookArray*)malloc(sizeof(BookArray));
			node->books->array=0;
			node->books->length=length;

			if(length != 0){
			Book *head=(Book*)malloc(sizeof(Book));
			Book *last=head;
			for(j=0;j<length;j++){
				fgets(buf, 100, fp);
				sscanf(buf,"id:%d\n", &id);
				fgets(buf, 100, fp);
				sscanf(buf,"title:%s\n",title);
				fgets(buf, 100, fp);
				sscanf(buf,"authors:%s\n",authors);
				fgets(buf, 100, fp);
				sscanf(buf,"year:%d\n", &year);
				fgets(buf, 100, fp);
				sscanf(buf,"copies:%d\n", &copies);
				fgets(buf, 100, fp);
				sscanf(buf, "loan:%d\n", &loan);

				Book *temp=(Book*)malloc(sizeof(Book));
				temp->id=id;
				temp->title=(char*)malloc(sizeof(strlen(title)));
				strcpy(temp->title,title);
				temp->authors=(char*)malloc(sizeof(strlen(authors)));
				strcpy(temp->authors,authors);
				temp->year=year;
				temp->copies=copies;
				temp->loan=loan;

				last->next=temp;
				last=temp;
			}
			last->next=NULL;
			node->books->array=head;
			}
			user_last->next=node;
			user_last=node;
    	}
		user_last->next=NULL;
	}
	fclose(fp);
	return 0;
}

//search for the account by username
//return 1 if found successfully, otherwise return 0
int match(const char *username)
{
	if(!strcmp(username,"librarian")){
		printf("\nSorry, registered unsuccessfully, the username you entered already exists.\n");
		return 1;
	}
	User *current=0;
	if(user_number!=0){
	for(current=user_head->next;current;current=current->next){
		if(current->username){
			if (!strcmp(current->username, username))
			{
				printf("\nSorry, registered unsuccessfully, the username you entered already exists.\n");
				return 1;
			}
		}
	}
	}
	return 0;
}

//return struct User if the password is correct, otherwise return 0
//if return 0 printf("Sorry, the usename or the password is incorrect.");
//otherwise printf("Welcome, %s", name);
User *verify(const char *username, const char *password)
{
	if(!strcmp(username,"librarian")&&!strcmp(password,"librarian")){
		printf("\nWelcome, %s", username);
		User *librarian=(User *)malloc(sizeof(User));
		librarian->username="librarian";
		return librarian;
	}
	User *current;
	if(user_number!=0){
	for(current=user_head->next;current;current=current->next){
		if (!strcmp(current->username, username) && !strcmp(current->password,password)){
				printf("\nWelcome, %s", current->name);
				return current;
			}
	}}
	printf("Sorry, the usename or the password is incorrect.\n");
	return 0;
}

void borrow_book(int id,User *user)
{
	Book *current;
	Book *temp;
	Book *last;
	for(current=book_head;current->next;current=current->next){
		if(current->next->id==id){
			if(current->next->copies==0){
				printf("Sorry, no copies.\n");
				return;
			}
			current->next->copies--;
			current->next->loan++;
			
			// add to user->book
			temp=(Book*)malloc(sizeof(Book));
			temp->id=current->next->id;
			temp->title=(char*)malloc(strlen(current->next->title));
			strcpy(temp->title,current->next->title);
			temp->authors=(char*)malloc(strlen(current->next->authors));
			strcpy(temp->authors,current->next->authors);
			temp->year=current->next->year;
			temp->copies=1;
			temp->loan=1;

			if(user->books->array==NULL){
				user->books->array=(Book*)malloc(sizeof(Book));
			}
			last=user->books->array;
			while(!last){
				last=last->next;
			}
			last->next=temp;
			last=temp;
			last->next=NULL;
			user->books->length++;
			printf("Borrow successfully.\n");
			return;
		}
	}
	printf("Please enter valid id.\n");
}

void return_book(int id,User *user)
{
	if(user->books->length==0){
		printf("No books you borrowed.\n");
		return;
	}
	Book *current;
	Book *temp;
	for(current=user->books->array;current->next;current=current->next){
		if(current->next->id=id){
			temp=current->next;
			current->next=current->next->next;
			add_book(*temp);
			free(temp);
			printf("Return successfully.\n");
			return;
		}
	}
	printf("Please enter valid id.\n");
}